package net.googlecode.garzha.task4;

import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
 * Created by IntelliJ IDEA.
 * User: Valera
 * Date: 05.04.11
 * Time: 15:16
 */

/**
 * Package string utilities
 * 1) Normalize string.
 * 2) Raise firsts letters every word.
 * 3) Rotate letters every word at the sentence.
 * 4) Replace all 'A' or 'a' at the sentence to exclamation mark.
 * 5) Add Date at the end of sentence.
 */
public class StringUtility {
    /**
     * Normalize string.
     *
     * @param str
     * @return
     */
    public String normalize(String str) {
        str = space(str);
        str = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
        return str;
    }

    /**
     * Delete extra spaces
     *
     * @param str
     * @return
     */
    private String space(String str) {
        str = str.trim();
        for (int i = 0; i < str.length(); i += 1) {
            if (str.substring(i, i + 1).equals(" ")) {
                int j = i;
                while (str.substring(j + 1, j + 2).equals(" ")) {
                    j += 1;
                }
                str = str.substring(0, i) + str.substring(j);
            }
        }
        return str;
    }

    /**
     * Raise firsts letters every word
     *
     * @param str
     * @return
     */
    public String raiseFirstLetters(String str) {
        boolean k = true;

        for (int i = 0; i < str.length(); i += 1) {

            if (!str.substring(i, i + 1).equals(" ") && k) {
                str = str.substring(0, i) + str.substring(i, i + 1).toUpperCase() + str.substring(i + 1);
                k = false;
            }
            if (str.substring(i, i + 1).equals(" ")) {
                k = true;
            }
        }
        return str;
    }

    /**
     * Rotate letters in one word
     *
     * @param str
     * @return
     */
    private String rotateWord(String str) {
        StringBuilder stringBuilder = new StringBuilder(str);
        return stringBuilder.reverse().toString();
    }

    /**
     * Rotate letters every word at the sentence
     *
     * @param str
     * @return
     */
    public String rotateSentence(String str) {
        /**
         * Variables beginning and end of words
         */
        int beg = 0;
        int end;
        /**
         *  Sensor beginning of the word
         */
        Boolean startword = false;
        /**
         * Cycle recalculation all characters in a string
         */
        for (int i = 0; i < str.length(); i += 1) {
            if (str.substring(i, i + 1).equals(".")) {
                end = i;
                startword = false;
                str = str.substring(0, beg) + rotateWord(str.substring(beg, end)) + str.substring(end);
                break;
            }

            if (!str.substring(i, i + 1).equals(" ") && !startword) {
                beg = i;
                startword = true;
            }
            if (str.substring(i, i + 1).equals(" ") && startword) {
                end = i;
                startword = false;
                str = str.substring(0, beg) + rotateWord(str.substring(beg, end)) + str.substring(end);
            }
        }
        return str;
    }

    /**
     * Replace all 'A' or 'a' at the sentence to exclamation mark
     *
     * @param str
     * @return
     */
    public String replaceAllAtoExclamationMark(String str) {
        /**
         * Cycle recalculation all characters in a string
         */
        for (int i = 0; i < str.length(); i += 1) {
            if ((str.substring(i, i + 1).equals("A")) || (str.substring(i, i + 1).equals("a"))) {
                str = str.substring(0, i) + "!" + str.substring(i + 1);
            }
        }
        return str;
    }

    /**
     * Add Date at the end of sentence
     * @param str
     * @return
     */
    public String addToEndDate(String str) {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -20);
        return str + String.valueOf(sDateFormat.format(calendar.getTime()));
    }
}
